package com.jojo.bubble.controller;

import com.jojo.bubble.entity.Agent;
import com.jojo.bubble.entity.Dev;
import com.jojo.bubble.entity.Shop;
import com.jojo.bubble.service.AgentService;
import com.jojo.bubble.common.domain.AjaxResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.util.ObjectUtils;
import com.jojo.bubble.common.enums.JsonResultEnum;

import javax.annotation.Resource;
import java.util.List;

/**
 * 代理商表(Agent)表控制层
 *
 * @author jojo
 * @since 2023-08-10 03:50:43
 */
@RestController
@RequestMapping("/admin/agent")
public class AgentController {
    /**
     * 服务对象
     */
    @Resource
    private AgentService agentService;


    @GetMapping("/list")
    public AjaxResult getList(Agent record) {
        List<Agent> agentList = agentService.getList(record);
        for (Agent agent : agentList) {
            if(agent.getSupAgentId()!=null){
                Agent agents = agentService.getInfo(agent.getSupAgentId());
                if (!ObjectUtils.isEmpty(agents)) {
                    agent.setSupAgentName(agents.getName());
                } else {
                    agent.setSupAgentName("未关联");
                }
            }
            else {
                agent.setSupAgentName("未关联");
            }

        }
        return AjaxResult.success(agentList);
    }

    @GetMapping("/info")
    public AjaxResult getInfo(Integer id) {
        Agent agent = agentService.getInfo(id);
        return AjaxResult.success(agent);
    }

    @PostMapping("/insert")
    public AjaxResult insert(Agent agent) {
        Agent record = agentService.queryByPhone(agent.getPhone());
        if (!ObjectUtils.isEmpty(record)) {
            return AjaxResult.error("手机号已注册");
        }
        agent.setStatus(2);
        agentService.insert(agent);
        return AjaxResult.success();
    }

    @PostMapping("/update")
    public AjaxResult update(Agent record) {
        if(record.getType()==4){
            record.setType(0);
            record.setLevel(1);
        }
        else{
            record.setLevel(0);
        }
        if(record.getSupAgentId().equals(record.getId()))
            return AjaxResult.error("不能设置自己为自己的上级");
        Agent setSupAgent = agentService.getInfo(record.getSupAgentId());
        if(setSupAgent!=null){
            if(setSupAgent.getSupAgentId()!=null && setSupAgent.getSupAgentId().equals(record.getId())){
                return AjaxResult.error("不能互相为互相的上级");
            }
        }
        Agent agent = agentService.getInfo(record.getId());
        if (ObjectUtils.isEmpty(agent)) {
            return AjaxResult.error(JsonResultEnum.RECORD_NULL);
        }
        agentService.update(record);
        return AjaxResult.success();
    }

    @PostMapping("/delete")
    public AjaxResult delete(int id) {
        agentService.delete(id);
        return AjaxResult.success();
    }
}

